package com.simtop.picture.controller;

import com.github.pagehelper.PageInfo;
import com.simtop.picture.common.ServerResponse;
import com.simtop.picture.pojo.*;
import com.simtop.picture.service.PictureService;
import com.simtop.picture.utils.ImagesUpload;
import com.simtop.picture.utils.PropertiesUtils;
import com.simtop.picture.utils.Thumbnail;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Controller
@CrossOrigin //todo 跨域问题
public class PictureController {

    @Autowired
    public PictureService pictureService;

    private Logger logger = LoggerFactory.getLogger(PictureController.class);

    private static String netPath = PropertiesUtils.getNetProperties();

    /**
     * 获取体验中心
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCenter", method = RequestMethod.GET)
    public ServerResponse<List<ExplorationCenter>> getCenter() {
        return pictureService.getCenter();
    }

    /**
     * 获取一级标题
     *
     * @param centerId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPrimaryTitle", method = RequestMethod.POST)
    public ServerResponse<List<PrimaryTitle>> getPrimaryTitle(@RequestBody String centerId) {
        JSONObject jsonObject = new JSONObject(centerId);
        return pictureService.getPrimaryTitle(jsonObject.getInt("explorationCenterId"));
    }

    /**
     * 获取二级标题
     *
     * @param primaryId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSecondTitle", method = RequestMethod.POST)
    public ServerResponse<List<SecondaryTitle>> getSecondTitle(@RequestBody String primaryId) {
        JSONObject jsonObject = new JSONObject(primaryId);
        return pictureService.getSecondTitle(jsonObject.getInt("primaryTitleId"));
    }

    /**
     * 添加一级标签
     *
     * @param primaryTitle
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addPrimaryTitle", method = RequestMethod.POST)
    public ServerResponse addPrimaryTitle(@RequestBody String primaryTitle) {
        JSONObject jsonObject = new JSONObject(primaryTitle);
        Integer explorationCenterId = jsonObject.getInt("explorationCenterId");//体验中心编号
        String primaryTitleName = jsonObject.getString("primaryTitleName");//一级编号
        return pictureService.addPrimaryTitle(explorationCenterId, primaryTitleName);
    }

    /**
     * 添加二级标签
     *
     * @param secondTitle
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addSecondTitle", method = RequestMethod.POST)
    public ServerResponse addSecondTitle(@RequestBody String secondTitle) {
        JSONObject jsonObject = new JSONObject(secondTitle);
        String secondaryTitleName = jsonObject.getString("secondaryTitleName");
        Integer primaryTitleId = jsonObject.getInt("primaryTitleId");
        return pictureService.addSecondTitle(primaryTitleId, secondaryTitleName);
    }

    /**
     * 删除一级标题
     *
     * @param primaryTitle
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deletePrimaryTitle", method = RequestMethod.POST)
    public ServerResponse deletePrimaryTitle(@RequestBody String primaryTitle) {
        JSONObject jsonObject = new JSONObject(primaryTitle);
        Integer primaryTitleId = jsonObject.getInt("primaryTitleId");
        return pictureService.deletePrimaryTitle(primaryTitleId);
    }

    /**
     * 删除二级标题
     *
     * @param secondTitle
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteSecondTitle", method = RequestMethod.POST)
    public ServerResponse deleteSecondTitle(@RequestBody String secondTitle) {
        JSONObject jsonObject = new JSONObject(secondTitle);
        Integer secondaryTitleId = jsonObject.getInt("secondaryTitleId");
        return pictureService.deleteSecondTitle(secondaryTitleId);
    }

    /**
     * 上传图片
     *
     * @param request
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/imagesUpload", method = RequestMethod.POST)
    public ServerResponse getImagesUpload(HttpServletRequest request, MultipartFile file, Integer secondaryTitleId, String pictureName) {
        String fileName = file.getOriginalFilename();
        String imagesPath = ImagesUpload.imagesUpload(file, request);
        long nums=pictureService.checkAddPictureName(pictureName,imagesPath);
       if(nums!=0){
           return ServerResponse.createByErrorMsg("该资源已经存在！");
       }
        String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (!("jpg".equalsIgnoreCase(suffixName) || "png".equalsIgnoreCase(suffixName))) {
            return ServerResponse.createByErrorMsg("请上传jpg或者png格式的图片文件");
        }
        if ((file.getSize() / 1024 / 1024) > 2) {
            return ServerResponse.createByErrorMsg("请上传图片小于2MB的图片文件");
        }
        PicturePath picturePath = new PicturePath();
        SecondaryTitle secondaryTitle=pictureService.getSecondaryTitleName(secondaryTitleId);
        if (!StringUtils.isEmpty(imagesPath)) {
            picturePath.setPicturePath(imagesPath);
            picturePath.setPictureName(pictureName);
            String targetPath = Thumbnail.thumbnail(imagesPath, imagesPath.substring(0, imagesPath.lastIndexOf("\\") + 1)+"\\"+secondaryTitle.getSecondaryTitleName()+"\\");
            logger.info("图片名称"+targetPath);
            if (targetPath != null) {
                picturePath.setPictureTargetPath(targetPath);
                picturePath.setSecondId(secondaryTitleId);
                int path = pictureService.addPicture(picturePath);
                if (path != 0) {
                    picturePath.setPicturePath(netPath + imagesPath.substring(imagesPath.lastIndexOf("\\") + 1));
                    picturePath.setPictureTargetPath(netPath + targetPath.substring(targetPath.lastIndexOf("\\\\") + 2));
                    return ServerResponse.createBySuccess("上传成功", picturePath);
                } else {
                    return ServerResponse.createByErrorMsg("上传失败");
                }
            } else {
                return ServerResponse.createByErrorMsg("上传失败");
            }

        } else {
            return ServerResponse.createByErrorMsg("上传失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getImage", method = RequestMethod.POST)
    public ServerResponse<PageInfo> getImage(@RequestBody String imageCondition) {
        JSONObject jsonObject = new JSONObject(imageCondition);
        Integer pageNum = jsonObject.getInt("pageNum");
        Integer pageSize = jsonObject.getInt("pageSize");
        Integer secondaryTitleId = jsonObject.getInt("secondaryTitleId");
        return pictureService.getImage(pageNum, pageSize, secondaryTitleId);
    }


    /**
     * 修改一级标签名
     *
     * @param primaryTitle
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updatePrimaryTitle", method = RequestMethod.POST)
    public ServerResponse<String> updatePrimaryTitle(@RequestBody String primaryTitle) {
        JSONObject jsonObject = new JSONObject(primaryTitle);
        Integer primaryTitleId = jsonObject.getInt("primaryTitleId");
        String primaryTitleName = jsonObject.getString("primaryTitleName");
        return pictureService.updatePrimaryTitle(primaryTitleId, primaryTitleName);
    }

    /**
     * 修改二级标签名
     *
     * @param secondTitle
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateSecondTitle", method = RequestMethod.POST)
    public ServerResponse<String> updateSecondTitle(@RequestBody String secondTitle) {
        JSONObject jsonObject = new JSONObject(secondTitle);
        Integer secondaryTitleId = jsonObject.getInt("secondaryTitleId");
        String secondaryTitleName = jsonObject.getString("secondaryTitleName");
        return pictureService.updateSecondTitle(secondaryTitleId, secondaryTitleName);
    }

    /**
     * 根据体验中心编号来获取图片列表
     *
     * @param picture
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPicture", method = RequestMethod.POST)
    public ServerResponse<PageInfo> getPicture(@RequestBody String picture) {
        JSONObject jsonObject = new JSONObject(picture);
        Integer pageNum = jsonObject.getInt("pageNum");
        Integer pageSize = jsonObject.getInt("pageSize");
        Integer explorationCenterId = jsonObject.getInt("explorationCenterId");
        return pictureService.getPicture(pageNum, pageSize, explorationCenterId);
    }

    /**
     * 根据图片编号标签编号删除图片
     *
     * @param picturePath
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deletePicturePath", method = RequestMethod.POST)
    public ServerResponse<String> deletePicturePath(@RequestBody String picturePath) {
        JSONObject jsonObject = new JSONObject(picturePath);
        Integer picturePathId = jsonObject.getInt("picturePathId");
        return pictureService.deletePicturePath(picturePathId);
    }


    /**
     * 编辑修改图片信息
     *
     * @param picturePathId
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/compilePicturePath", method = RequestMethod.POST)
    public ServerResponse compilePicturePath(Integer picturePathId, Integer secondId, MultipartFile file, HttpServletRequest request, String pictureName) {
        long nums=pictureService.checkPictureName(pictureName,picturePathId);
        if(nums!=0){
            return ServerResponse.createByErrorMsg("该资源已经存在！");
        }
        String fileName ="";
        PicturePath picturePath = new PicturePath();
        if(file==null){
            picturePath.setPicturePathId(picturePathId);
            picturePath.setPictureName(pictureName);
            picturePath.setSecondId(secondId);
            int path = pictureService.compilePicturePath(picturePath);
            if (path != 0) {
                return ServerResponse.createBySuccess("修改成功", picturePath);
            } else {
                return ServerResponse.createByErrorMsg("修改失败");
            }
        }
        fileName = file.getOriginalFilename();
        String imagesPath = ImagesUpload.imagesUpload(file, request);
        long imageNums=pictureService.checkImagePath(imagesPath,picturePathId);
        if(imageNums!=0){
            return ServerResponse.createByErrorMsg("该资源已经存在！");
        }
        String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (!("jpg".equalsIgnoreCase(suffixName) || "png".equalsIgnoreCase(suffixName))) {
            return ServerResponse.createByErrorMsg("请上传jpg或者png格式的图片文件");
        }
        if ((file.getSize() / 1024 / 1024) > 2) {
            return ServerResponse.createByErrorMsg("请上传图片小于2MB的图片文件");
        }

        SecondaryTitle secondaryTitle=pictureService.getSecondaryTitleName(secondId);

        if (!StringUtils.isEmpty(imagesPath)) {
            picturePath.setPicturePath(imagesPath);
            picturePath.setPictureName(pictureName);
            String targetPath = Thumbnail.thumbnail(imagesPath, imagesPath.substring(0, imagesPath.lastIndexOf("\\") + 1)+"\\"+secondaryTitle.getSecondaryTitleName()+"\\");
            if (targetPath != null) {
                picturePath.setPictureTargetPath(targetPath);
                picturePath.setPicturePathId(picturePathId);
                if (!StringUtils.isEmpty(secondId)) {
                    picturePath.setSecondId(secondId);
                }
                int path = pictureService.compilePicturePath(picturePath);
                if (path != 0) {
                    picturePath.setPicturePath(netPath + imagesPath.substring(imagesPath.lastIndexOf("\\") + 1));
                    picturePath.setPictureTargetPath(netPath + targetPath.substring(targetPath.lastIndexOf("\\\\") + 2));
                    return ServerResponse.createBySuccess("修改成功", picturePath);
                } else {
                    return ServerResponse.createByErrorMsg("修改失败");
                }
            } else {
                return ServerResponse.createByErrorMsg("修改失败");
            }

        } else {
            return ServerResponse.createByErrorMsg("上传失败");
        }
    }

    /**
     * 多条件筛选图片资源
     *
     * @param filterPicture
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/filterPicture", method = RequestMethod.POST)
    public ServerResponse<PageInfo> filterPicture(@RequestBody String filterPicture) {
        JSONObject jsonObject = new JSONObject(filterPicture);
        Integer pageNum = jsonObject.getInt("pageNum");
        Integer pageSize = jsonObject.getInt("pageSize");
        Integer secondId = jsonObject.getInt("secondId");
        return pictureService.filterPicture(pageNum, pageSize, secondId);
//        Integer primaryTitleId=jsonObject.getInt("primaryTitleId");
//        return  pictureService.filterPicture(pageNum,pageSize,secondId,primaryTitleId);
    }


    @RequestMapping(value = "/create",method = RequestMethod.GET)
    public void create(){
        List<SecondaryTitle> secondaryTitleList=pictureService.create();
        secondaryTitleList.forEach(secondaryTitle -> {
            String secondaryTitleName = secondaryTitle.getSecondaryTitleName();
            File file=new File("F:\\image\\StreamingAssets\\",secondaryTitleName);
            if(!file.exists()){
                file.mkdir();
            }
        });
    }

    /**
     * 根据图片编号获取图片信息
     * @param pictureDetail
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPictureDetail",method = RequestMethod.POST)
    public ServerResponse getPictureDetail(@RequestBody String pictureDetail){
        JSONObject jsonObject=new JSONObject(pictureDetail);
        Integer picturePathId=jsonObject.getInt("picturePathId");
        return pictureService.getPictureDetail(picturePathId);
    }


    @ResponseBody
    @RequestMapping(value = "/getU3dPath",method = RequestMethod.GET)
    public List<U3dPath> getU3dPath(){
        return pictureService.getU3dPath();
    }


}



//    @ResponseBody
//    @RequestMapping(value = "/getPrimaryTitle",method = RequestMethod.POST)
//    public void getPrimaryTitle(@RequestBody String centerId){
//        JSONObject jsonObject=new JSONObject(centerId);
//        logger.info("一级编号"+jsonObject.getInt("centerId"));
//        return pictureService.getPrimaryTitle();
//    }


